Imports System
Imports System.IO

Public Class Frm_Filter

    Private FrmName1 As String

    Public Sub LoadHeaderCaption(ByVal Fields As String, ByVal FrmName As String)
        Try
            Dim FieldName As String = ""
            FrmName1 = FrmName
            Dim Str() As String = Fields.Split("&")
            Dim CheckBox As CheckBox
            Dim Ds As New DataSet
            Dim m As Integer
            Dim X, Y, H As Integer
            Dim RowCount As Integer = 1
            Dim Size As New Size(80, 16)
            Dim Point As Point
            Dim j As Integer = 0
            For i As Integer = 0 To Str.Length - 2
                If i = 0 Then
                    X = 10
                    Y = 15
                Else
                    If (i Mod 6) = 0 Then
                        X = 10
                        Y += 34
                        RowCount += 1
                    Else
                        X += 80
                    End If
                End If

                Point.X = X
                Point.Y = Y

                CheckBox = New CheckBox
                CheckBox.Size = Size
                CheckBox.Location = Point
                CheckBox.TextAlign = ContentAlignment.BottomCenter
                CheckBox.Text = Str(i).Split("^")(0)
                CheckBox.BackColor = Color.Transparent
                Me.GroupBox1.Controls.Add(CheckBox)
                AddHandler CheckBox.CheckedChanged, AddressOf CheckBox_Click
                j += 1
            Next

            Me.Height = RowCount * 30 + 90
            Me.GroupBox1.Height = RowCount * 25 + 40
            Dim mPoint As Point
            Dim nPoint As Point
            Dim xPoint As Point
            mPoint.X = 5
            mPoint.Y = RowCount * 27 + 40
            nPoint.X = 142
            nPoint.Y = RowCount * 27 + 40
            xPoint.X = 275
            xPoint.Y = RowCount * 27 + 40
            Me.CheckBox1.Location = mPoint
            Me.BtnOk.Location = nPoint
            Me.BtnCancel.Location = xPoint

            Ds = ReadXml(FrmName)

            If Not Ds Is Nothing Then

                For i As Integer = 0 To Ds.Tables(0).Rows.Count - 1
                    If Ds.Tables(0).Rows(i).Item("HeaderVisible").ToString = "True" Then
                        CType(Me.GroupBox1.Controls(i), CheckBox).Checked = True
                        m += 1
                    Else
                        CType(Me.GroupBox1.Controls(i), CheckBox).Checked = False
                    End If
                Next

            End If
            If m = Me.GroupBox1.Controls.Count Then
                Me.CheckBox1.Checked = True
            Else
                Me.CheckBox1.Checked = False
            End If
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub CheckBox_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Dim i As Integer = 0
            For Each mCheck As CheckBox In Me.GroupBox1.Controls
                If mCheck.Checked = True Then
                    i += 1
                End If
            Next
            If i = Me.GroupBox1.Controls.Count Then
                Me.CheckBox1.Checked = True
            Else
                Me.CheckBox1.Checked = False
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub


    Private Sub CheckBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.Click
        Try
            If Me.CheckBox1.Checked = True Then
                For Each mCheck As CheckBox In Me.GroupBox1.Controls
                    mCheck.Checked = True
                Next
            Else
                For Each mCheck As CheckBox In Me.GroupBox1.Controls
                    mCheck.Checked = False
                Next
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub Frm_Filter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub BtnOk_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnOk.Click
        Try
            Dim FiledName As String = ""
            Dim Ds As New DataSet
            For Each mCheck As CheckBox In Me.GroupBox1.Controls
                FiledName += mCheck.Text & "^" & mCheck.Checked & "&"
            Next
            Ds = GetGroups(FiledName)
            WriteXml(Ds, FrmName1)
            Me.Close()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Close()
    End Sub
End Class